package com.yc.deadletter.customer;

import com.rabbitmq.client.Channel;
import com.yc.deadletter.config.MqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信消费者
 */
@Component
public class DeadCustomer {


    /**
     * 死信队列a的消费者
     * @param channel
     * @param message
     */
    @RabbitListener(queues = MqConfig.DEAD_LETTER_QUEUEA_NAME)
    public void deadCustomerA(Channel channel, Message message){

        byte [] b = message.getBody();
        String msg= new String(b);

        System.out.println("死信队列a的消费者 = " + msg);

        try {
            //确认后删除
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 死信队列b的消费者
     * @param channel
     * @param message
     */
    @RabbitListener(queues = MqConfig.DEAD_LETTER_QUEUEB_NAME)
    public void deadCustomerB(Channel channel, Message message){

        byte [] b = message.getBody();
        String msg= new String(b);

        System.out.println("死信队列b的消费者 = " + msg);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
